﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class JLTVKHL : DocumentBase
    {
        #region=============Properties======================= 
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        bool IsSetUp = false; 
        JLTVKHLData m_Data;
 
        public DataSet setResult;
        JLTVKHLReport m_rpt;
        string UserID = "";
        #region==========CollectionViewSource=================
        SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)this.FindResource("SetData");
            }
        }
 
        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if (vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        } 

        CollectionViewSource vwVKKOPF
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwVKKOPF");
            }
        } 
        string art
        {
            get
            {
                if (dwVKPOS == null)
                    return "";
                return dwVKPOS["VKPART"].ToString();

            }
        }
        #endregion
        int total = 0;
        public JLTVKHL()
        {
            InitializeComponent();
            this.Name = "手织板";
            ZoneAccount.Visibility = Visibility.Hidden;
            ZoneAuditing.Visibility = Visibility.Hidden;
            ZoneOrder.Visibility = Visibility.Hidden;
        } 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                m_Data = new JLTVKHLData(); 

                #region============TESTINGDATA================
                txtVKPAUFKRS.Text = "H" + m_Data.date.Substring(3, 1);
                txtUserID.Text = app.User;
                txtPassword.Password = app.Pwd;
                #endregion
                btnReport.IsEnabled = true;
                btnPrintPreview.IsEnabled = true;
                btnGetVKPOS.IsEnabled = false;  
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        } 
        
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent(); 
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            ZoneAccount.Visibility = Visibility.Hidden;
            ZoneAuditing.Visibility = Visibility.Hidden;
            ZoneOrder.Visibility = Visibility.Hidden;
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            txtLogIn.Background = new SolidColorBrush(Colors.Yellow);
            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(SPassword);
            string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            DataRow dwHR = app.UserValid(userID, Password);
            if (dwHR == null)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            expLog.IsExpanded = false;
            //expLog.Visibility = Visibility.Hidden;
            btnReport.IsEnabled = true;
            btnPrintPreview.IsEnabled = true;
            btnGetVKPOS.IsEnabled = true;
            btnDistribute.IsEnabled = false;
            IsBelongs2V9.IsChecked = false;
            IsOnAuditing.IsChecked = false;
            IsSendForAuditing.IsChecked = true;
            ZoneOrder.Visibility = Visibility.Visible;
            ZoneAccount.Visibility = Visibility.Visible; 
            EventDispatcher.Instance.RaiseStatus(txtLogIn.Text, StatusEventType.UserLogIn);
        } 
        #endregion
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PrintType = "JustExcel";
                GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
        private void btnDistribute_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType, true);
        }
        public void GenerateReport(string PrintType, bool isapproved = false)
        {
            try
            {                
                JLTVKHLReport m_rpt = new JLTVKHLReport();
                m_rpt.dsSource = SetData;
                m_rpt.PrintType = PrintType; 
                m_rpt.IsApproved = isapproved;
                m_rpt.UserID = this.txtUserID.Text;
                 
                if (IsOnAuditing.IsChecked == true)
                {
                    m_rpt.IsOnAuditing = true;
                }
                if (IsSendForAuditing.IsChecked == true)
                {
                    m_rpt.IsSendForAuditing = true;
                }
                m_rpt.WriteExcelReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnGetVKPOS_Click(object sender, RoutedEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
            try
            {
                SetData.Clear();
                DataSet setResult = new DataSet();
                using (new LongOperation(this, "读取符合条件的记录"))
                { 
                    setResult = m_Data.BuildData("00",m_Data.Left(txtVKPAUFKRS.Text + "  ", 2),m_Data.Right("0000000" + txtVKPAUFLFD.Text, 6));
                    SetData.Merge(setResult);
                    if (vwVKPOS.View != null)
                    {
                        vwVKPOS.View.MoveCurrentToFirst();
                    }
                }
                btnReport.IsEnabled = true;
                btnPrintPreview.IsEnabled = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            EventDispatcher.Instance.RaiseStatus("查询结束", StatusEventType.StopProgress);
        }

        
    }
}
